﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;
using WMSABL;
using WMSABL.SY;
using WMSAML.SY; 

namespace ElectronicWMSA.SY
{
    public partial class InitialPasswordFrm : Form
    {
        public InitialPasswordFrm()
        {
            InitializeComponent();
            if (SystemVariable.User_ID == 1 || SystemVariable.User_ID == 2)
            {
            }
            else
            {
                MessageClass.warning("此操作只允许管理设定！");
                this.Close();
            }
        }
        private string _StrWhere = "";
        private void InitialPasswordFrm_Load(object sender, EventArgs e)
        {
            _StrWhere = "";
            pageControlGroupUserList.PageCurrent = 1;
            pageControlGroupUserList.PageSize = 50;
            pageControlGroupUserList.Bind();
        }

        public int DBind_dataGridViewUserList()
        {
            ElectronicWMSA.Comm.Forms.MsControls.PageData pageData = new ElectronicWMSA.Comm.Forms.MsControls.PageData();
            pageData.TableName = "Vlh_TUserListByGroupid";
            pageData.PrimaryKey = "User_id";
            pageData.OrderStr = "User_id";
            pageData.PageIndex = this.pageControlGroupUserList.PageCurrent;
            pageData.PageSize = this.pageControlGroupUserList.PageSize;
            pageData.QueryCondition = _StrWhere;
            pageData.QueryFieldName = "*";

            this.pageControlGroupUserList.bindingSource.DataSource = pageData.QueryDataTable().Tables[0];
            this.pageControlGroupUserList.bindingNavigator.BindingSource = pageControlGroupUserList.bindingSource;
            this.dataGridViewUserList.AutoGenerateColumns = false;
            this.dataGridViewUserList.DataSource = this.pageControlGroupUserList.bindingSource;
            SystemVariable.DataGridViewStyle(dataGridViewUserList);
            this.dataGridViewUserList.ClearSelection();
            return pageData.TotalCount;
        }

        private int pageControlGroupUserList_EventPaging(ElectronicWMSA.Comm.Forms.MsControls.EventPagingArg e)
        {
            return DBind_dataGridViewUserList();
        }

        private void dataGridViewUserList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dataGridViewUserList.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dataGridViewUserList.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }

        private void dataGridViewUserList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (SystemVariable.User_ID == 1 || SystemVariable.User_ID == 2)
            {
                if (this.dataGridViewUserList.Rows.Count != 0)
                {
                    if (e.RowIndex > -1)
                    {
                        if (e.ColumnIndex != -1 && this.dataGridViewUserList.CurrentCell.ColumnIndex == 0)
                        {
                            if (!SystemVariable.VaildationPermission("编辑")) return;
                            int uid = Convert.ToInt32(dataGridViewUserList.CurrentRow.Cells["User_id"].Value.ToString());
                            TUsers_BLL bll_TUsers = new TUsers_BLL();
                            TUsers_Entity entity = new TUsers_Entity();
                            entity = bll_TUsers.GetEntityByID(uid);
                            if (entity.User_id == 2 && SystemVariable.User_ID != 2)
                            {
                                MessageClass.warning("此用户密码不能被修改！");
                                return;
                            }
                            InitialPwdFrm frm = new InitialPwdFrm(entity.User_id);
                            frm.ShowDialog();
                        }
                    }
                }
            }
            else
            {
                MessageClass.warning("没有权限，请联系管理员！");
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("查询")) return;

            string userName = this.txtUserName.Text.Trim();
            if (userName != "")
            {
                _StrWhere = " Login_name like '%" + userName + "%' OR Username  like '%" + userName + "%'";
            }
            else
            {
                _StrWhere = "";
            }
            this.pageControlGroupUserList.Bind();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
